package com.example.yls.demoa;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity
implements IOnUpdateListener{

    private MyReceiver receiver1;
    private NetReceiver receiver2;
    private TextView text_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        receiver1 = new MyReceiver(MainActivity.this);
        receiver2 = new NetReceiver();

        IntentFilter filter1 = new IntentFilter();
        filter1.addAction(Intent.ACTION_TIME_CHANGED);
        filter1.addAction(Intent.ACTION_TIME_TICK);
        filter1.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        registerReceiver(receiver1,filter1);

        IntentFilter filter2 = new IntentFilter();
        filter2.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver2,filter2);

        text_1 = findViewById(R.id.text_1);
        updateTime();

    }

    @Override
    public void updateTime() {
        Calendar calendar = Calendar.getInstance();
        int h = calendar.get(Calendar.HOUR);
        int m = calendar.get(Calendar.MINUTE);
        text_1.setText(h+":"+m);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver1);
        unregisterReceiver(receiver2);
        super.onDestroy();
    }
}
